package uit.nfc.nfcmenudishes;

import uit.nfc.utils.CommentListAdapter;
import uit.nfc.utils.DishesItem;
import uit.nfc.utils.GetDishesByIdAsyncTask;
import uit.nfc.utils.NfcUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IntroActivity extends Activity {
	
	private TextView textTable;
	private TextView textDishes;
	private EditText editQuantity;
	private Button buttonOrder;
	
	private ImageView imageView;
	
	private ListView listComment;
	
	private EditText editName;
	private EditText editComment;
	private Button buttonPost;
	
	private String table;
	private String dishesId;
	private DishesItem dishes;
	private Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);
		
		textTable = (TextView)findViewById(R.id.text_table);
		textDishes = (TextView)findViewById(R.id.text_dishes);
		editQuantity = (EditText)findViewById(R.id.edit_quantity);
		buttonOrder = (Button)findViewById(R.id.button_order);
		
		imageView = (ImageView)findViewById(R.id.image);
		
		listComment = (ListView)findViewById(R.id.list_comments);
		listComment.setAdapter(new CommentListAdapter());
		
		editName = (EditText)findViewById(R.id.edit_name);
		editComment = (EditText)findViewById(R.id.edit_comment);
		buttonPost = (Button)findViewById(R.id.button_post_comment);
		
		context = this;
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		NfcUtils.disableTagWriteMode(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		NdefMessage message = NfcUtils.readTag(this.getIntent());
		if (message != null) {
			String prefix = new String(message.getRecords()[0].getPayload());
			if (prefix.equals(NfcMenuActivity.PREFIX) && (message.getRecords().length > 2)) {
				table = new String(message.getRecords()[1].getPayload());
				dishesId = new String(message.getRecords()[2].getPayload());
				
				handleValidTag();
				
				return;
			}
		}
		
		handleInvalidTag();

		NfcUtils.enableTagWriteMode(this);
	}
	
	private void handleValidTag() {
		this.textTable.setText(table);
		
		new GetDishesByIdAsyncTask(context, dishesId).execute();
	}
	
	private void handleInvalidTag() {
		String notice = "Invalid tag! Do you want write a dishes for this tag?";
		
		AlertDialog dialog = new AlertDialog.Builder(context).create();
		dialog.setTitle("Notice");
		dialog.setMessage(notice);
		
		dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Yes", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				Intent sellTicket = new Intent(IntroActivity.this, NfcMenuActivity.class);
				startActivity(sellTicket);
				
				finish();
			}
		});
		
		dialog.setButton(DialogInterface.BUTTON_POSITIVE, "No", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
		
		dialog.show();
	}

	public TextView getTextDishes() {
		return textDishes;
	}

	public ImageView getImageView() {
		return imageView;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_intro, menu);
		return true;
	}
}
